<?php

namespace app\common\model\banxi\learn;

use app\common\model\banxi\student\Student;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 全科习题答题记录
 */
class LearnSayAntiForgetting extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_say_anti_forgetting';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'useTimeData'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];
    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getUseTimeDataAttr($value, $data){
        $value = $value?? ($data['useTime']??'');
        return secondToTime($value);
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    /**
     * 将记录按日期分组包装
     *
     * @param array $trainerList 记录列表
     * @return array
     */
    public function wrapperReviewDate($trainerList)
    {
        $result = [];

        // 按日期进行分组
        $dateGroupMap = [];
        foreach ($trainerList as $item) {
            $date = $item['date'];
            if (!isset($dateGroupMap[$date])) {
                $dateGroupMap[$date] = [];
            }
            $dateGroupMap[$date][] = $item;
        }

        // 构造 ReviewDate 对象
        foreach ($dateGroupMap as $date => $values) {
            $reviewDate = [
                'date' => $date,
                'recordList' => $values
            ];
            $result[] = $reviewDate;
        }

        // 按日期正序排序
        usort($result, function ($a, $b) {
            return strcmp($b['date'],$a['date']);
        });

        return $result ?? [];
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function student()
    {
        return $this->hasOne(Student::class, 'id', 'studentId');
    }
    public function trainerInfo()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerAntiForgettingId');
    }

}